package gunglad.com.gitee_22_5_25_test12

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.recommendation.ALS
import org.apache.spark.mllib.recommendation.Rating

object MovieRecommand {


  def main(args: Array[String]): Unit = {
    val sc = new SparkContext(new SparkConf().setMaster("local[2]").setAppName("movie"))

    val dataRdd: RDD[String] = sc.textFile("hdfs://192.168.0.101:9000/ml-100k/u.data")
    val dataRdds = dataRdd.map(_.split("\t").take(3))
    dataRdds.first()
    val ratings = dataRdds.map { case Array(user, movie, rating) => Rating(user.toInt, movie.toInt, rating.toDouble) }
    ratings.first()
    val model = ALS.train(ratings, 50, 10, 0.01)
    val predictedRating = model.predict(100, 200)
    // 定义用户id
    val userid = 100
    // 定义推荐数量
    val num = 10
    val topRecoPro = model.recommendProducts(userid, num)


    val moviesRdd = sc.textFile("hdfs://192.168.0.101:9000/ml-100k/u.item")

    val titles = moviesRdd.map(line => line.split("\\|").take(2)).map(array
    => (array(0).toInt, array(1))).collectAsMap()
    topRecoPro.map(rating => (titles(rating.product), rating.rating)).foreach(println)


    val a= model.recommendUsers(100, 5)
    a.toArray.foreach(println)

  }



}
